import {Component} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {Location} from '@angular/common';
import 'rxjs/add/operator/switchMap';
import {HttpClient} from '@angular/common/http';
import {AppUrl} from '../../../app.url';
import {NzMessageService} from 'ng-zorro-antd';
import {HttpError} from '../../../common/http-error';

@Component({
    templateUrl: './sponInfo.component.html',
    styleUrls: ['../spon/spon.less', '../../_home/home.less', './sponInfo.less']
})

export class SponInfoComponent {
    message: string;
    private supInfo = {
        commodity: '',
        authForView: '',
        city: '',
        student: {},
        type: '',
        percent: 0,
        feedback: '',
        id: 0,
        eDate: '',
        amount: 0,
        propaganda: '',
        article: {},
        population: '',
        applicant: [],
        sDate: '0',
        topic: ''
    };
    private business = {   //赞助发起者（企业）信息
        qq: '',
        authForView: '',
        introduce: '',
        sex: '',
        businessName: '',
        cityId: '',
        busiLevel: '',
        businessUserId: '',
        realName: '',
        busiScale: '',
        position: '',
        addr: '',
        email: ''
    };
    private articleHtml = {//赞助中的文章
        articleId: '',
        type: 0,
        uploadTime: '',
        enabled: 0,
        content: ''
    };
    private applyReason = '';//申请理由
    private applicant = [];//已申请人id
    private applicantCount = 0;//已申请人数
    private userSponList = [
        {
            supId: '',
            supName: '',
            supMoney: ''
        }
    ];

    constructor(private location: Location,
                private route: ActivatedRoute,
                private http: HttpClient,
                private nzMessage: NzMessageService,
                private httpError: HttpError) {
    }

    advertise: any[] = [
        {img: './assets/img/coffee1.jpg'},
        {img: './assets/img/coffee2.jpg'},
        {img: './assets/img/coffee3.jpg'},
        {img: './assets/img/coffee4.jpg'}
    ];

    hottor: any[] = [
        '赞助全国高校社团活动',
        '千元赞助，可签合同',
        '以纯A21高校活动赞助',
        '扫码关注公众号并分享',
        '【APP下载推广】全国社团最大APP'
    ];

    ngOnInit(): void {
        let supId = +this.route.snapshot.params.id;
        if (supId == null || supId == undefined) {
            this.nzMessage.create('error', '数据获取失败，请返回重试', {nzDuration: 4000});
        } else {
            this.http.post(AppUrl.support.selectInfoUrl, {id: supId}).subscribe(data => {
                let temp: any = data;
                if (temp.code == 200) {
                    this.supInfo = temp.data;
                    this.articleHtml = temp.data.article;
                    this.business = temp.data.business;
                    this.applicant = temp.data.applicant;
                    this.applicantCount = this.applicant.length;
                    this.getPage(1);
                }
                else if (temp.code == 300)
                    this.nzMessage.error('你要查询的赞助不见啦', {nzDuration: 4000});
            }, data => this.httpError.getError(data));
        }
    }

    //申请赞助事件
    applySupport(): void {
        this.http.post(AppUrl.support.applyUrl, {
            bsId: this.supInfo.id,
            sId: localStorage.getItem('userid'),
            reason: this.applyReason
        }).subscribe(data => {
            let temp: any = data;
            if (temp.code == 200)
                this.nzMessage.create('success', '申请成功', {nzDuration: 4000});
            else if (temp.code == 400)
                this.nzMessage.create('warning', '您已经申请过了', {nzDuration: 4000});
        }, data => this.httpError.getError(data));
    }

    //获取我的赞助申请
    myApplySupport(): void {
        this.http.post(AppUrl.support.myApplyUrl, {sId: localStorage.getItem('userid')}).subscribe(data => {
            let temp: any = data;
            console.log(temp.data);
        }, data => this.httpError.getError(data));
    }

    private isVisible = false;

    showModal = () => {
        this.isVisible = true;
    };

    handleOk = (e) => {
        this.isVisible = false;
    };

    handleCancel = (e) => {
        console.log(e);
        this.isVisible = false;
    };

    private page: any = {
        pageNum: 1,
        pageSize: 3,
        total: 0,
    };

    getPage(pageNum) {
        if (this.business.businessUserId != '') {
            this.http.post(AppUrl.support.selectWhoseListUrl, {
                pageNo: pageNum,
                pageSize: this.page.pageSize,
                bId: this.business.businessUserId
            }).subscribe(data => {
                let temp: any = data;
                if (temp.code == 200) {
                    this.page = temp.data;
                    this.userSponList = this.page.list;
                }
            }, data => {
                this.httpError.getError(data);
            }, () => {
            });
        }
    }
}
